/*     */ package com.zimbra.cs.service.admin;
/*     */ 
/*     */ import com.zimbra.common.account.Key.DomainBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.accesscontrol.AdminRight;
/*     */ import com.zimbra.cs.account.accesscontrol.Rights.Admin;
/*     */ import com.zimbra.soap.JaxbUtil;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.admin.message.CheckDomainMXRecordRequest;
/*     */ import com.zimbra.soap.admin.message.CheckDomainMXRecordResponse;
/*     */ import com.zimbra.soap.admin.type.DomainSelector;
/*     */ import com.zimbra.soap.admin.type.DomainSelector.DomainBy;
/*     */ import java.util.Hashtable;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.naming.NameNotFoundException;
/*     */ import javax.naming.NamingEnumeration;
/*     */ import javax.naming.NamingException;
/*     */ import javax.naming.directory.Attribute;
/*     */ import javax.naming.directory.Attributes;
/*     */ import javax.naming.directory.DirContext;
/*     */ import javax.naming.directory.InitialDirContext;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CheckDomainMXRecord
/*     */   extends AdminDocumentHandler
/*     */ {
/*     */   public boolean domainAuthSufficient(Map context)
/*     */   {
/*  43 */     return true;
/*     */   }
/*     */   
/*     */ 
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  50 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  51 */     Provisioning prov = Provisioning.getInstance();
/*  52 */     CheckDomainMXRecordRequest req = (CheckDomainMXRecordRequest)JaxbUtil.elementToJaxb(request);
/*  53 */     Key.DomainBy domainBy = req.getDomain().getBy().toKeyDomainBy();
/*  54 */     String value = req.getDomain().getKey();
/*     */     
/*  56 */     Domain domain = prov.get(domainBy, value);
/*     */     
/*  58 */     checkDomainRight(zsc, domain, Rights.Admin.R_checkDomainMXRecord);
/*     */     
/*  60 */     String SMTPHost = domain.getAttr("zimbraDNSCheckHostname", true);
/*  61 */     String domainName = domain.getName();
/*  62 */     if ((SMTPHost == null) || (SMTPHost.length() < 1)) {
/*  63 */       SMTPHost = domain.getAttr("zimbraSmtpHostname", false);
/*     */     }
/*  65 */     if ((SMTPHost == null) || (SMTPHost.length() < 1)) {
/*  66 */       SMTPHost = prov.getLocalServer().getAttr("zimbraSmtpHostname");
/*     */     }
/*  68 */     if ((SMTPHost == null) || (SMTPHost.length() < 1)) {
/*  69 */       SMTPHost = prov.getConfig().getAttr("zimbraSmtpHostname");
/*     */     }
/*  71 */     if ((SMTPHost == null) || (SMTPHost.length() < 1)) {
/*  72 */       SMTPHost = domain.getName();
/*     */     }
/*  74 */     String SMTPHostMatch = String.format("^\\d+\\s%s\\.$", new Object[] { SMTPHost });
/*  75 */     ZimbraLog.soap.info("checking domain mx record");
/*  76 */     Hashtable<String, String> env = new Hashtable();
/*  77 */     env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
/*  78 */     String message = String.format("Domain is configured to use SMTP host: %s. None of the MX records match this name.", new Object[] { SMTPHost });
/*     */     
/*  80 */     CheckDomainMXRecordResponse resp = new CheckDomainMXRecordResponse();
/*  81 */     boolean found = false;
/*     */     try {
/*  83 */       DirContext ictx = new InitialDirContext(env);
/*  84 */       Attributes attrs = ictx.getAttributes(domainName, new String[] { "MX" });
/*  85 */       if (attrs.size() < 1) {
/*  86 */         throw ServiceException.FAILURE("NoMXRecordsForDomain", null);
/*     */       }
/*  88 */       for (NamingEnumeration<? extends Attribute> ne = attrs.getAll(); ne.hasMore();) {
/*  89 */         Attribute attr = (Attribute)ne.next();
/*  90 */         if (attr.size() == 1) {
/*  91 */           ZimbraLog.soap.info("single attribute");
/*  92 */           Object o = attr.get();
/*  93 */           if ((o instanceof String)) {
/*  94 */             String rec = o.toString();
/*  95 */             ZimbraLog.soap.info("found MX record " + rec);
/*  96 */             if (rec.matches(SMTPHostMatch)) {
/*  97 */               found = true;
/*  98 */               break;
/*     */             }
/* 100 */             resp.addEntry(rec);
/*     */           } else {
/* 102 */             String rec = new String((byte[])o);
/* 103 */             ZimbraLog.soap.info("found MX attribute " + attr.getID() + " = " + rec);
/* 104 */             if (rec.matches(SMTPHostMatch)) {
/* 105 */               found = true;
/* 106 */               break;
/*     */             }
/* 108 */             resp.addEntry(rec);
/*     */           }
/*     */         }
/*     */         else {
/* 112 */           ZimbraLog.soap.info("multivalued attribute");
/* 113 */           for (int i = 0; i < attr.size(); i++) {
/* 114 */             Object o = attr.get(i);
/* 115 */             if ((o instanceof String)) {
/* 116 */               String rec = o.toString();
/* 117 */               ZimbraLog.soap.info("found MX record " + attr.getID() + "-" + Integer.toString(i) + " = " + rec);
/* 118 */               if (rec.matches(SMTPHostMatch)) {
/* 119 */                 found = true;
/* 120 */                 break;
/*     */               }
/* 122 */               resp.addEntry(rec);
/*     */             } else {
/* 124 */               String rec = new String((byte[])o);
/* 125 */               ZimbraLog.soap.info("found MX attribute " + attr.getID() + "-" + Integer.toString(i) + " = " + rec);
/* 126 */               if (rec.matches(SMTPHostMatch)) {
/* 127 */                 found = true;
/* 128 */                 break;
/*     */               }
/* 130 */               resp.addEntry(rec);
/*     */             }
/*     */           }
/*     */         }
/*     */       }
/*     */       
/*     */ 
/* 137 */       if (found) {
/* 138 */         resp.setCode("Ok");
/*     */       } else {
/* 140 */         resp.setCode("Failed");
/* 141 */         resp.setMessage(message);
/*     */       }
/*     */     } catch (NameNotFoundException e) {
/* 144 */       throw ServiceException.FAILURE("NameNotFoundException", e);
/*     */     }
/*     */     catch (NamingException e) {
/* 147 */       throw ServiceException.FAILURE("Failed to verify domain's MX record. " + e.getMessage(), e);
/*     */     }
/* 149 */     return zsc.jaxbToElement(resp);
/*     */   }
/*     */   
/*     */   public void docRights(List<AdminRight> relatedRights, List<String> notes)
/*     */   {
/* 154 */     relatedRights.add(Rights.Admin.R_checkDomainMXRecord);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/admin/CheckDomainMXRecord.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */